<?php session_start(); ?>

<?php

include "Connect.php";

?>

<?php

$food = @$_POST['food'];
$snacks = @$_POST['snacks'];
$services = @$_POST['services'];
$drinks = @$_POST['drinks'];
$delete = @$_POST['delete'];
$enterRecord = @$_POST['enterRecord'];

$select = @$_POST['select'];
$rN = @mysql_real_escape_string($_POST['recordName']);
$recordName = ucfirst($rN);
$recordPrice = @mysql_real_escape_string($_POST['recordPrice']);

if($food){

showFood();

 }
else if($drinks){

showDrinks();

}else if($snacks){

showSnacks();

}
else if($services){
showServices();
 }
 
if(isset($delete)){

$checkbox=@$_POST['checkbox'];
for($i=0;$i<count($checkbox);$i++){
$del_id = $checkbox[$i];


if($Clicked="food"){
$sql = "DELETE FROM food WHERE ID ='$del_id'";
$result = mysql_query($sql);
$mP = $_SESSION['food'] = "food";

}if($Clicked="drinks"){
$sql = "DELETE FROM drinks WHERE ID ='$del_id'";
$result = mysql_query($sql);

$mP = $_SESSION['drinks'] = "drinks";


}if($Clicked="snacks"){
$sql = "DELETE FROM snacks WHERE ID ='$del_id'";
$result = mysql_query($sql);
$mP = $_SESSION['snacks'] = "snacks";


}if($Clicked="services"){
$sql = "DELETE FROM services WHERE ID ='$del_id'";
$result = mysql_query($sql);
$mP = $_SESSION['services'] = "services";


}
  }

if($result){

if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1; 
$max_results = 4;

$from = (($page * $max_results) - $max_results);

$getData = mysql_query("SELECT * FROM "./*$Clicked*/$mP." LIMIT $from, $max_results") or die(mysql_error());
$stuff = "Drinks";
$G = $Clicked;
$_SESSION['G'] = $mP;

if(!mysql_num_rows($getData)>0){
$msg =  "No data found, ".$mP." table Empty";
header("location:dataColl.php?poll=$msg");
}else{
include "dataColl.php";
}
#---------------------------------------------------------------------------------------------------------------------

}
 else{
 $msg = "No data to delete!!";
header("location:dataColl.php?poll=$msg");
 
 }
   }
 
if(isset($enterRecord)){
  
if($select=="---Select record type---" || $recordName == "Record Name:" || $recordPrice == "Record Price:"){


$msg2 = "Please Enter all fields!!";
header("location:dataColl.php?poll2=$msg2");
  }else{
  
if($select=="Food"){

$cntr = mysql_num_rows(mysql_query("SELECT * FROM food"))+ 1;

$Qw = mysql_query("SELECT * FROM food WHERE Name = '$recordName' ") or die(mysql_error());

$k = mysql_fetch_array($Qw);
if(mysql_num_rows($Qw)>0){

$msg = $recordName." already exists in the database!!"; 
header("location:dataColl.php?poll=$msg");
}else{

$s  = mysql_query("INSERT INTO food VALUES($cntr,'$recordName','$recordPrice')") or die(mysql_error());
showFood();
include "dataColl.php";

}

  }
  
if($select=="Drinks"){

$cntr = mysql_num_rows(mysql_query("SELECT * FROM drinks"))+ 1;

$Qw = mysql_query("SELECT * FROM drinks WHERE Name = '$recordName' ") or die(mysql_error());

$k = mysql_fetch_array($Qw);
if(mysql_num_rows($Qw)>0){

$msg = $recordName." already exists in the database!!"; 
header("location:dataColl.php?poll=$msg");
showFood();
}else{

$s  = mysql_query("INSERT INTO drinks VALUES($cntr,'$recordName','$recordPrice')") or die(mysql_error());
showFood();
include "dataColl.php";

}
  }
if($select=="Snacks"){


$cntr = mysql_num_rows(mysql_query("SELECT * FROM snacks"))+ 1;

$Qw = mysql_query("SELECT * FROM snacks WHERE Name = '$recordName' ") or die(mysql_error());

$k = mysql_fetch_array($Qw);
if(mysql_num_rows($Qw)>0){

$msg = $recordName." already exists in the database!!"; 
header("location:dataColl.php?poll=$msg");
showFood();
}else{

$s  = mysql_query("INSERT INTO snacks VALUES($cntr,'$recordName','$recordPrice')") or die(mysql_error());
showFood();
include "dataColl.php";

}
  }
if($select=="Services"){

$cntr = mysql_num_rows(mysql_query("SELECT * FROM services"))+ 1;

$Qw = mysql_query("SELECT * FROM services WHERE Name = '$recordName' ") or die(mysql_error());

$k = mysql_fetch_array($Qw);
if(mysql_num_rows($Qw)>0){

$msg = $recordName." already exists in the database!!"; 
header("location:dataColl.php?poll=$msg");
showFood();
}else{

$s  = mysql_query("INSERT INTO services VALUES($cntr,'$recordName','$recordPrice')") or die(mysql_error());
showFood();
include "dataColl.php";

}

  }  
  
  else{

  }
  
 }
   }
   
if (isset($_GET['page']) and $_SESSION['G'] == "food"){
 $page = $_GET['page'];
 showFood();
     }
 
function showFood(){

if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1; 

@$pageFor = "food";
$max_results = 4;

$from = (($page * $max_results) - $max_results);
$getData = mysql_query("SELECT * FROM food LIMIT $from, $max_results") or die("Failed to Query the Food table");
$_SESSION['hG'] = $Clicked = "food";
$stuff = "Foods";
$G = "Food";
$_SESSION['G'] = "food";

if(!mysql_num_rows($getData)>0){
$msg =  "No data found, Food table Empty";
header("location:dataColl.php?poll=$msg");
}else{
$msg =  "Some data found";
include "dataColl.php";

} 
   }
  
if (isset($_GET['page'])  and $_SESSION['G'] == "drinks"){
$page = $_GET['page'];
showDrinks();
 }

function showDrinks(){
if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1; 

@$pageFor = "drinks";
$max_results = 4;

$from = (($page * $max_results) - $max_results);
$getData = mysql_query("SELECT * FROM drinks LIMIT $from, $max_results") or die("Failed to Query the Drinks table");
$_SESSION['hG'] = $Clicked="drinks";
$stuff = "Drinks";
$G = "Drink";
$_SESSION['G'] = "drinks";

if(!mysql_num_rows($getData)>0){
$msg =  "No data found, Drinks table Empty";
header("location:dataColl.php?poll=$msg");
}else{
$msg =  "Some data found";
include "dataColl.php";
}

}

if (isset($_GET['page'])  and $_SESSION['G'] == "snacks"){
$page = $_GET['page'];
showSnacks();
 }

function showSnacks(){
if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1; 

@$pageFor = "snacks";
$max_results = 4;

$from = (($page * $max_results) - $max_results);
$getData = mysql_query("SELECT * FROM snacks LIMIT $from, $max_results") or die("Failed to Query the Snacks table");
$_SESSION['hG'] = $Clicked="snacks";
$stuff = "Snacks";
$G = "Snack";
$_SESSION['G'] = "snacks";

if(!mysql_num_rows($getData)>0){

$msg =  "No data found, Snacks table Empty";
header("location:dataColl.php?poll=$msg");
}else{
$msg =  "Some data found";
include "dataColl.php";
}

}

if (isset($_GET['page'])  and $_SESSION['G'] == "services"){
$page = $_GET['page'];
showServices();
 }
 
function showServices(){

if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1; 

@$pageFor = "services";
$max_results = 4;

$from = (($page * $max_results) - $max_results);
$getData = mysql_query("SELECT * FROM services LIMIT $from, $max_results") or die("Failed to Query the Services table");
$_SESSION['hG'] = $Clicked="services";

$stuff = "Services";
$G = "Service";
$_SESSION['G'] = "services";

if(!mysql_num_rows($getData)>0){
$msg =  "No data found, Services table Empty";
header("location:dataColl.php?poll=$msg");
}else{
$msg =  "Some data found";
include "dataColl.php";
}
}

$edit = @$_POST['edit'];
if(isset($edit)){

echo $hiddenField = $_POST['h'];
  }

?>
